package myapp.googlemap;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;

public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem>  {

	private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	private boolean isDragAndDrop = false;
	private Context mContext;
    private Drawable marker=null;
    private ImageView dragImage=null;
    private OverlayItem inDrag=null;
    private int xDragImageOffset=0;
    private int yDragImageOffset=0;
    private int xDragTouchOffset=0;
    private int yDragTouchOffset=0;
    private GeoPoint geoPoint = null;
    
	public MyItemizedOverlay(Drawable defaultMarker) {
		super(boundCenterBottom(defaultMarker));
		// TODO Auto-generated constructor stub
		marker = defaultMarker;
	}
	
	public MyItemizedOverlay(Drawable defaultMarker, Context context) {
		super(boundCenterBottom(defaultMarker));
		// TODO Auto-generated constructor stub
		marker = defaultMarker;
		mContext = context;	    
	}
	
	public MyItemizedOverlay(Drawable defaultMarker, Context context, ImageView image, boolean isDragAndDrop) {
		super(boundCenterBottom(defaultMarker));
		// TODO Auto-generated constructor stub
		marker = defaultMarker;
		mContext = context;
		this.isDragAndDrop = isDragAndDrop;
		
		dragImage=image;
		xDragImageOffset=dragImage.getDrawable().getIntrinsicWidth()/2;
		yDragImageOffset=dragImage.getDrawable().getIntrinsicHeight();
	    
	}

	public void UpdateListOverlayItem(int index, OverlayItem item)
	{
		mOverlays.set(index, item);
	}
	public void SetDragAndDrop(boolean isDragAndDrop)
	{
		this.isDragAndDrop = isDragAndDrop;
	}
	
	public GeoPoint GetGeoPoint()
	{
		return this.geoPoint;
	}
	
	public void SetGeoPoint(GeoPoint g)
	{
		this.geoPoint = g;
	}
	
	
	@Override
	protected OverlayItem createItem(int i) {
		// TODO Auto-generated method stub
		return mOverlays.get(i);
	}

	@Override
	public int size() {
		// TODO Auto-generated method stub
		return mOverlays.size();
	}
	
	public void addOverlay(OverlayItem overlay) {
		geoPoint = overlay.getPoint();
	    mOverlays.add(overlay);
	    populate();
	}
	
	public void RemoveOverlay(int index)
	{
		mOverlays.remove(index);
		populate();
	}
	
	
	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		// TODO Auto-generated method stub
		if (!shadow)
		{
			super.draw(canvas, mapView, shadow);
		}
	}
	

	public boolean onTap(GeoPoint p, MapView mapView) { 
		  Projection projection = mapView.getProjection();
		  Point pointTap=new Point(0,0);
		  projection.toPixels(p, pointTap);
		  
		  int scaledSize = mContext.getResources().getDimensionPixelSize(R.dimen.textSizeUnit);		  
		  Rect bounds = new Rect(0, 0, 48*scaledSize, 48*scaledSize);//marker.getBounds();
		  int offsetX = 48*scaledSize/2;
		  int offsetY = 48*scaledSize/2;

		  for (int i = 0; i < mOverlays.size(); i++)
		  {
			  Point pointItem=new Point(0,0);
			  projection.toPixels(mOverlays.get(i).getPoint(), pointItem);
			  
			  if (bounds.contains(pointItem.x - pointTap.x + offsetX, pointItem.y - pointTap.y + offsetY))
			  {
					AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
					dialog.setTitle(mOverlays.get(i).getTitle());
					dialog.setMessage(mOverlays.get(i).getSnippet());
					dialog.show();
				    return true;
			  }
		  }
		  return false;
		}
/*	
	@Override
	protected boolean onTap(int index) {
		OverlayItem item = mOverlays.get(index);		
		AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
		dialog.setTitle(item.getTitle());
		dialog.setMessage(item.getSnippet());
		dialog.show();
		Log.i("ItemizedOverlay","onTap");
		return super.onTap(index);
	}*/
	
	@Override
	public boolean onTouchEvent(MotionEvent event, MapView mapView) {
        boolean result=false;
		  
        int scaledSize = mContext.getResources().getDimensionPixelSize(R.dimen.textSizeUnit);
        Rect bounds = new Rect(0, 0, 48*scaledSize, 48*scaledSize);
        int offsetX = 48*scaledSize/2;
        int offsetY = 48*scaledSize/2;
		if (isDragAndDrop == true)
		{
			int action = event.getAction(); 
	        
	        int x = (int)event.getX(); 
	        int y = (int)event.getY();   
	
	        if (action==MotionEvent.ACTION_DOWN) {
	              OverlayItem item = mOverlays.get(0);//Hand point icon
	              Point p=new Point(0,0);
	              
	              mapView.getProjection().toPixels(item.getPoint(), p);
	              
	              //if (hitTest(item, marker, x-p.x, y-p.y)) {
	              if (bounds.contains(p.x - x + offsetX, p.y - y + offsetY)){
		                result=true;
		                inDrag=item;
		                mOverlays.remove(inDrag);
		                populate();
		
		                xDragTouchOffset=0;
		                yDragTouchOffset=0;
		                
		                setDragImagePosition(p.x, p.y);
		                dragImage.setVisibility(View.VISIBLE);
		
		                xDragTouchOffset=x-p.x;
		                yDragTouchOffset=y-p.y; 
		                Log.i("MyItemized-Hand",item.getTitle());
		                Log.i("MyItemized-Hand-x",String.valueOf(xDragTouchOffset));
		                Log.i("MyItemized-Hand-y",String.valueOf(yDragTouchOffset));		                
	              }
              }
	          else if (action==MotionEvent.ACTION_MOVE && inDrag!=null)
	          {
	        	  setDragImagePosition(x, y);
	        	  result=true;
	          }
	          	else if (action==MotionEvent.ACTION_UP && inDrag!=null) {
	          		dragImage.setVisibility(View.GONE);
	            
	          		GeoPoint pt=mapView.getProjection().fromPixels(x-xDragTouchOffset,
	                                                       y-yDragTouchOffset);
	          		OverlayItem toDrop=new OverlayItem(pt, inDrag.getTitle(),
	                                               inDrag.getSnippet());
	            
		            mOverlays.add(toDrop);
		            populate();
	            
		            inDrag=null;
		            geoPoint = pt;
		            result=true;
	          }
		}
          
        return(result || super.onTouchEvent(event, mapView));
	}
    private void setDragImagePosition(int x, int y) {
        RelativeLayout.LayoutParams lp=
          (RelativeLayout.LayoutParams)dragImage.getLayoutParams();
              
        lp.setMargins(x-xDragImageOffset-xDragTouchOffset,
                        y-yDragImageOffset-yDragTouchOffset, 0, 0);
        dragImage.setLayoutParams(lp);
    }

}
